/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package estrutura;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Turma de alguma disciplina disponibilizada pela Unicamp.
 * @author Gustavo
 */

@Entity
@Table(name = "TURMA")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Turma.findAll", query = "SELECT t FROM Turma t"),
    @NamedQuery(name = "Turma.findByTurma", query = "SELECT t FROM Turma t WHERE t.turma = :turma")})
public class Turma implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * ID da turma.
     */
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    /**
     * Sigla que identifica a turma.
     */
    @Column(name = "TURMA")
    private String turma;
    @JoinTable(name = "HORARIOSTURMAS", joinColumns = {
        @JoinColumn(name = "ID_TURMA", referencedColumnName = "ID")}, inverseJoinColumns = {
        @JoinColumn(name = "PERIODO", referencedColumnName = "PERIODO"),
        @JoinColumn(name = "DIA", referencedColumnName = "DIA"),
        @JoinColumn(name = "INICIO", referencedColumnName = "INICIO"),
        @JoinColumn(name = "FIM", referencedColumnName = "FIM")})
    /**
     * Horários da turma.
     */
    @ManyToMany
    private Collection<Horario> horarioCollection;
    
    /**
     * Constroi uma turma sem definir a sigla
     */
    public Turma() {
    }
    
    /**
     * Constroi uma turma com sigla
     */
    public Turma(String turma) {
        this.turma = turma;
    }
    
    /**Retorna a sigla da turma
     * @return String - Sigla da turma
     */
    public String getTurma() {
        return turma;
    }
    
    /**Define sigla da turma*/
    public void setTurma(String turma) {
        this.turma = turma;
    }

    @XmlTransient
    /**Retorna a lista de horários da turma
     * @return Horario - Lista de horários
     */
    public Collection<Horario> getHorarioCollection() {
        return horarioCollection;
    }
    
    /**Define a lista de horários*/
    public void setHorarioCollection(Collection<Horario> horarioCollection) {
        this.horarioCollection = horarioCollection;
    }
    
    /**Cria uma lista de horários para ser inserida*/
    public void addHorario(Horario horario) {
        if (horarioCollection == null) {
            horarioCollection = new LinkedList<>();
        }
        horarioCollection.add(horario);
    }
    
    /**Cria um horários para ser inserido*/
    public void addHorario(int periodo, int dia, int inicio, int fim) {
        if (horarioCollection == null) {
            horarioCollection = new LinkedList<>();
        }
        horarioCollection.add(new Horario(periodo, dia, inicio, fim));
    }
    
    /**
     * Retorna o id da turma.
     * @return Retorna o id da turma.
     */
    public int getId() {
        return id;
    }
    
    /**
     * Seta o id da turma
     * @param id Id da turma
     */
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (turma != null ? turma.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Turma)) {
            return false;
        }
        Turma other = (Turma) object;
        return (this.turma == null || this.turma.equals(other.turma)) && (this.turma != null || other.turma == null);
    }

    @Override
    public String toString() {
        return "estrutura.Turma_1[ turma=" + turma + " ]";
    }
    
}
